/*	Copyright 2013 RangerUFO
 *
 *	This file is part of Libevent-helper.
 *
 *	Libevent-helper is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU Lesser General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.
 *
 *	Libevent-helper is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU Lesser General Public License for more details.
 *
 *	You should have received a copy of the GNU Lesser General Public License
 *	along with Libevent-helper.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "event2/acceptor.h"
#include "event2/session.h"
#include "event2/helper.h"
#include <event2/listener.h>
#include <arpa/inet.h>
#include <errno.h>
#include <string.h>

struct evacceptor
{
	struct evconnlistener* lev;
	evacceptor_cb accept_cb;
	evacceptor_error_cb error_cb;
	void* args;
};

static void lev_accept_cb(struct evconnlistener* lev, evutil_socket_t fd, struct sockaddr* sa, int len, void* args)
{
	struct evacceptor* ac = (struct evacceptor*)args;
	struct evsession* s;

	if (ac->accept_cb && (s = evsession_new(evconnlistener_get_base(lev), fd)))
	{
		ac->accept_cb(ac, s, ac->args);
	}
	else
	{
		evutil_closesocket(fd);
	}
}

static void lev_error_cb(struct evconnlistener* lev, void* args)
{
	struct evacceptor* ac = (struct evacceptor*)args;

	if (ac->error_cb)
	{
		ac->error_cb(ac, ac->args);
	}
}

struct evacceptor* evacceptor_new_bind(struct event_base* base, const char* ip, int port, int backlog)
{
	struct evacceptor* ac = evhelper_malloc(sizeof(struct evacceptor));

	if (!ac)
	{
		return 0;
	}

	struct sockaddr_in sin;

	memset(&sin, 0, sizeof(sin));
	sin.sin_family = AF_INET;

	if (ip)
	{
		sin.sin_addr.s_addr = inet_addr(ip);
	}
	else
	{
		sin.sin_addr.s_addr = htonl(0);
	}

	sin.sin_port = htons(port);

	struct evconnlistener* lev = evconnlistener_new_bind(base, lev_accept_cb, ac, LEV_OPT_CLOSE_ON_FREE | LEV_OPT_REUSEABLE | LEV_OPT_THREADSAFE, backlog, (struct sockaddr*)&sin, sizeof(sin));

	if (!lev)
	{
		evhelper_free(ac);
		return 0;
	}

	evconnlistener_set_error_cb(lev, lev_error_cb);

	memset(ac, 0, sizeof(struct evacceptor));
	ac->lev = lev;

	return ac;
}

void evacceptor_free(struct evacceptor* ac)
{
	if (!ac)
	{
		return;
	}

	evconnlistener_free(ac->lev);
	evhelper_free(ac);
}

struct event_base* evacceptor_get_base(struct evacceptor* ac)
{
	if (!ac)
	{
		return 0;
	}

	return evconnlistener_get_base(ac->lev);
}

void evacceptor_setcb(
		struct evacceptor* ac,
		evacceptor_cb accept_cb,
		evacceptor_error_cb error_cb,
		void* args)
{
	if (ac)
	{
		ac->accept_cb = accept_cb;
		ac->error_cb = error_cb;
		ac->args = args;
	}
}

int evacceptor_enable(struct evacceptor* ac)
{
	if (!ac)
	{
		return -1;
	}

	return evconnlistener_enable(ac->lev);
}

int evacceptor_disable(struct evacceptor* ac)
{
	if (!ac)
	{
		return -1;
	}

	return evconnlistener_disable(ac->lev);
}
